<!DOCTYPE html>
<body>
<script src="../../../resources/testharness.js"></script>
<script src="../../../resources/testharnessreport.js"></script>
<script>

function checkDataTypeAgainstStorageFormat(data, storageFormat) {
  if (storageFormat == "uint8")
    assert_equals(Object.prototype.toString.call(data), "[object Uint8ClampedArray]");
  else if (storageFormat == "uint16")
    assert_equals(Object.prototype.toString.call(data), "[object Uint16Array]");
  else if (storageFormat == "float32")
    assert_equals(Object.prototype.toString.call(data), "[object Float32Array]");
}

function checkColorSettings(colorSettings, colorSpace, storageFormat) {
  assert_equals(colorSettings.colorSpace, colorSpace);
  assert_equals(colorSettings.storageFormat, storageFormat);
}

function runTest(colorSettings, expectedColorSettings) {
  var canvas = document.createElement("canvas");
  var ctx = canvas.getContext("2d");

  imageData = ctx.createImageData(1, 1, colorSettings);
  checkDataTypeAgainstStorageFormat(imageData.data, colorSettings.storageFormat);
  assert_array_equals(imageData.data, [0, 0, 0, 0]);
  checkColorSettings(imageData.getSettings(), expectedColorSettings.colorSpace, expectedColorSettings.storageFormat);
}

var testScenarios = [
	["Test default color settings: {undefined, undefined} -> {srgb, uint8}", {}, {colorSpace: "srgb", storageFormat: "uint8"}],

	["Test default color space: {undefined, float32} -> {srgb, float32}", {storageFormat: "float32"}, {colorSpace: "srgb", storageFormat: "float32"}],
	["Test default storage format: {srgb, undefined} -> {srgb, uint8}", {colorSpace: "srgb"}, {colorSpace: "srgb", storageFormat: "uint8"}],

	["Test color settings: {srgb, uint8}", {colorSpace: "srgb", storageFormat: "uint8"}, {colorSpace: "srgb", storageFormat: "uint8"}],
	["Test color settings: {srgb, uint16}", {colorSpace: "srgb", storageFormat: "uint16"}, {colorSpace: "srgb", storageFormat: "uint16"}],
	["Test color settings: {srgb, float32}", {colorSpace: "srgb", storageFormat: "float32"}, {colorSpace: "srgb", storageFormat: "float32"}],

	["Test color settings: {rec2020, uint8}", {colorSpace: "rec2020", storageFormat: "uint8"}, {colorSpace: "rec2020", storageFormat: "uint8"}],
	["Test color settings: {rec2020, uint16}", {colorSpace: "rec2020", storageFormat: "uint16"}, {colorSpace: "rec2020", storageFormat: "uint16"}],
	["Test color settings: {rec2020, float32}", {colorSpace: "rec2020", storageFormat: "float32"}, {colorSpace: "rec2020", storageFormat: "float32"}],

	["Test color settings: {display-p3, uint8}", {colorSpace: "display-p3", storageFormat: "uint8"}, {colorSpace: "display-p3", storageFormat: "uint8"}],
	["Test color settings: {display-p3, uint16}", {colorSpace: "display-p3", storageFormat: "uint16"}, {colorSpace: "display-p3", storageFormat: "uint16"}],
	["Test color settings: {display-p3, float32}", {colorSpace: "display-p3", storageFormat: "float32"}, {colorSpace: "display-p3", storageFormat: "float32"}],

];

generate_tests(runTest, testScenarios);

</script>
</body>
